package com.iseastar.dianxiaosan.coupon;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.CouponBean;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import java.util.ArrayList;

import droid.frame.activity.title.TitleRes;
import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * 我的邮票列表
 *
 * @author coffee<br>
 *         2015-7-2下午2:38:31
 */
public class CouponListActivity extends BaseActivity2 {

    private SListView mListView;
    private CouponListAdapter mAdapter;
    // 邮票ID
    private int couponId;
    // 订单费用
    private String cost;
    private String from;

    private ReqResult<CouponBean> result;

    private LayoutInflater mLayoutInflater = null;
    private TextView mTipsContentTV;
    private View mHeadView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getIntent() != null) {
            couponId = getIntent().getIntExtra("couponId", -1);
            cost = getIntent().getStringExtra("cost");
            from = getIntent().getStringExtra("from");
        }
        super.onCreate(savedInstanceState);
        // showLoadingBar();
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.coupon_list);
        super.findViewById();
        TextView mTitleRight = (TextView) findViewById(R.id.title_right_text);
        mTitleRight.setTextColor(Color.parseColor("#6F8FAF"));
        mLayoutInflater = LayoutInflater.from(this);

        getAppTitle().setCommonTitle("我的邮票", new TitleRes("失效邮票", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getContext(), CouponExpireListActivity.class);
                startActivity(intent);
            }
        }));
        mListView = (SListView) findViewById(R.id.listview);
        mListView.setDivider(null);
        mHeadView = mLayoutInflater.inflate(R.layout.layout_tips, null);
        mTipsContentTV = (TextView) mHeadView.findViewById(R.id.tipscontentTV);
        mListView.addHeaderView(mHeadView);
        //
        ArrayList<CouponBean> items = new ArrayList<CouponBean>();
        mAdapter = new CouponListAdapter(items, getContext());
        mAdapter.setShowFooter(from == null);
        mAdapter.setSelectId(couponId);
        //
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 从订单支付界面条转过来的
                if (cost != null) {
                    try {
                        CouponBean item = (CouponBean) parent.getItemAtPosition(position);
                        Intent data = new Intent();
                        data.putExtra("item", item);
                        setResult(RESULT_OK, data);
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //
        mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().newCouponList(cost);
            }
        });
        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                if (result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().newCouponList(cost);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 回到界面刷新
        AppHttp.getInstance().newCouponList(cost);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 查询邮票列表
            case MsgID.couponList:
                onRefreshComplete();
                result = JSON.parseList(msg.obj, CouponBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mListView.getAdapter() == null) {
                                mListView.setAdapter(mAdapter);
                            }
                            if (result.getPageNum() <= 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            if (mHeadView != null) {
                                if (mAdapter.getItemsSize() > 0) {
                                    mHeadView.setVisibility(View.VISIBLE);
                                    mTipsContentTV.setText(String.format(getString(R.string.mystamptips), mAdapter.getItemsSize()));
                                } else {
                                    mHeadView.setVisibility(View.GONE);
                                }
                            }
                            mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }
}
